<template>
  <div>
    <div class="title">
      <p>订单列表</p>
      <el-button class="dele" type="" size="mini" @click="deleAllchose">
        批量删除
      </el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="orderList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="order" label="订单编号" width="120" align="center">
      </el-table-column>
      <el-table-column prop="name" label="用户微信" width="120" align="center">
      </el-table-column>
      <el-table-column
        prop="phone.phone"
        label="会员手机"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="coursename"
        label="课程名称"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="money" label="订单金额" width="120" align="center">
      </el-table-column>
      <el-table-column label="订单状态" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ typeObj[scope.row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" width="160">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
            >查看</el-button
          >
          <el-button
            type="warning"
            size="mini"
            @click="eidtMsg(scope.row)"
            icon="el-icon-edit"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="info"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
            >取消</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="修改信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="用户电话" prop="phone.phone">
          <el-input v-model="ruleForm.phone.phone"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="coursename">
          <el-input v-model="ruleForm.coursename"></el-input>
        </el-form-item>

        <el-form-item label="订单金额" prop="money">
          <el-input v-model="ruleForm.money"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
      <!-- <el-button :plain="true" @click="open2">成功</el-button> -->
    </el-dialog>
  </div>
</template>


<script>
import { mapState } from "vuex";
export default {
  // props:['orderArr'],
  data() {
    return {
      typeObj: {
        0: "待支付",
        1: "已关闭",
        2: "待确认",
        3: "学习中",
        4: "已完成",
      },

      multipleSelection: [],
      dialogVisible: false,

      ruleForm: {
        name: "",
        phone: {},
        coursename: "",
        money: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入用户姓名", trigger: "change" },
          { min: 2, max: 8, message: "长度在 2 到 8个字符", trigger: "change" },
        ],
        phone: [{ required: true, message: "请输入用户电话", trigger: "blur" }],
        coursename: [
          { required: true, message: "请输入所购课程名称", trigger: "change" },
        ],
        money: [
          { required: true, message: "请输入订单金额", trigger: "change" },
        ],
      },
    };
  },

  computed: {
    ...mapState(["orderList"]),
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete(id) {
      this.$store.commit("cancelOrder", id);
       this.$message('删除成功');
    },
    handleEdit(row) {
      this.$router.push({
        name: "goodsDetails",
        // path:'/OrderGoods/orderDetails',
        params: row,
      });
      this.$parent.show = false;
    },
    deleAllchose() {
      var ids = "";
      this.multipleSelection.forEach((item) => {
        ids += item.id;
      });
      this.$store.commit("deleteChange", ids);
       this.$message('删除成功');
    },

    eidtMsg(row) {
      this.dialogVisible = true;
      this.ruleForm = { ...row };
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.commit("eidtOrder", this.ruleForm);
          this.dialogVisible = false;
           this.$message({
            message: "恭喜你，修改成功",
            type: "success",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  background: white;
  justify-content: flex-start;
  border-top: 20px solid #d5dff3;
  p {
    margin-left: 30px;
    font-size: 800;
  }
  .dele {
    border: 0;
    margin-left: 20px;
    font-size: 15px;
  }
}
</style>